import {GET_HOME_DATA,GET_MORE_LIST,CHANGE_SHOW_TOP} from './actionTypes';
import axios from 'axios';

const dataAction=(data)=>{
    return {
        type:GET_HOME_DATA,
        topicList:data.topicList,
        articlList:data.articlList,
        recommendList:data.recommendList,
    }
}

const moreListAction=(data,page)=>{
    return {
        type:GET_MORE_LIST,
        data,
        page,
    }
}

export const homeDataAction=()=>{
    return (dispatch)=>{
        axios.get('https://www.fastmock.site/mock/eaa7a18e424aefd6b9ee29850969298e/api/home').then((res)=>{
            const data=res.data.data;
            const action=dataAction(data);
            dispatch(action);
        }).catch(()=>{
            console.log('error')
        })
    }
}

export const getMoreListAction=(page)=>{
    return (dispatch)=>{
        axios.get(`https://www.fastmock.site/mock/eaa7a18e424aefd6b9ee29850969298e/api/home/more?page=${page}`).then(res=>{
            const data=res.data.data;
            const action=moreListAction(data,page);
            dispatch(action)
        })
    }
}

export const changeShowTop=(show)=>{
    return {
        type:CHANGE_SHOW_TOP,
        show
    }
}